package com.john.demo.bio;

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

import static com.john.demo.bio.SocketUtils.readLine;
import static com.john.demo.bio.SocketUtils.writeLine;

public class SocketClient {
	Socket socket;

	public static void main(String[] args) {
		SocketClient sc = new SocketClient();
		sc.startClient();
	}

	public void startClient() {
		try {
			socket = new Socket("192.168.1.4", 10099);

			System.out.println("客户端启动，请输入任意内容，按'q'退出...");

			Scanner sc = new Scanner(System.in);

			// 读消息线程
			new Thread(()->{
				while(true) {
					String receiveMsg = readLine(socket);
					if(receiveMsg != null) {
						System.out.println("服务端：" + receiveMsg);
					}
				}
			}).start();
			// 写消息线程
			new Thread(()->{
				while(true) {
					String writeMsg = sc.nextLine();
					writeLine(socket, writeMsg);
				}
			}).start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
